//
//  HHBaseTableListView.m
//  developKit
//
//  Created by Hayder on 2022/12/18.
//

#import "HHBaseTableListView.h"

@implementation HHBaseTableListView

- (instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame])
    {
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.tableView registerClass:[HHBaseTableListCell class] forCellReuseIdentifier:NSStringFromClass(HHBaseTableListCell.class)];
        [self.tableView registerClass:[HHNormalTableListCell class] forCellReuseIdentifier:NSStringFromClass(HHNormalTableListCell.class)];
        [self.tableView registerClass:[HHSwitchTableListCell class] forCellReuseIdentifier:NSStringFromClass(HHSwitchTableListCell.class)];
        [self.tableView registerClass:[HHTextFieldTableListCell class] forCellReuseIdentifier:NSStringFromClass(HHTextFieldTableListCell.class)];
        [self.tableView registerClass:[HHButtonTableListCell class] forCellReuseIdentifier:NSStringFromClass(HHButtonTableListCell.class)];
        [self.tableView registerClass:[HHImageTableListCell class] forCellReuseIdentifier:NSStringFromClass(HHImageTableListCell.class)];
        [self.tableView registerClass:[HHSendCodeTableListCell class] forCellReuseIdentifier:NSStringFromClass(HHSendCodeTableListCell.class)];
    }
    return self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HHBaseListItem *item = self.dataSource[indexPath.row];
    HHBaseTableListCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(HHBaseTableListCell.class)];
    if([item isKindOfClass:[HHNormalListItem class]])
    {
        HHNormalListItem *normalItem = (HHNormalListItem *)item;
        cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(HHNormalTableListCell.class)];
        HHNormalTableListCell *normalCell = (HHNormalTableListCell *)cell;
        normalCell.item = normalItem;
    }else if([item isKindOfClass:[HHSwitchListItem class]])
    {
        HHSwitchListItem *switchItem = (HHSwitchListItem *)item;
        cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(HHSwitchTableListCell.class)];
        HHSwitchTableListCell *switchCell = (HHSwitchTableListCell *)cell;
        switchCell.item = switchItem;
    }else if([item isKindOfClass:[HHTextFieldListItem class]])
    {
        HHTextFieldListItem *textFieldItem = (HHTextFieldListItem *)item;
        cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(HHTextFieldTableListCell.class)];
        HHTextFieldTableListCell *textfieldCell = (HHTextFieldTableListCell *)cell;
        textfieldCell.item = textFieldItem;
    }else if([item isKindOfClass:[HHButtonListItem class]])
    {
        HHButtonListItem *buttonItem = (HHButtonListItem *)item;
        cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(HHButtonTableListCell.class)];
        HHButtonTableListCell *bttonCell = (HHButtonTableListCell *)cell;
        bttonCell.item = buttonItem;
    }else if([item isKindOfClass:[HHImageListItem class]])
    {
        HHImageListItem *imageItem = (HHImageListItem *)item;
        cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(HHImageTableListCell.class)];
        HHImageTableListCell *imageCell = (HHImageTableListCell *)cell;
        imageCell.item = imageItem;
    }else if([item isKindOfClass:[HHSendCodeListItem class]])
    {
        HHSendCodeListItem *sendCodeItem = (HHSendCodeListItem *)item;
        cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(HHSendCodeTableListCell.class)];
        HHSendCodeTableListCell *sendCodeCell = (HHSendCodeTableListCell *)cell;
        sendCodeCell.item = sendCodeItem;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HHBaseListItem *item = self.dataSource[indexPath.row];
    return item.cellHeight;
}

- (CGFloat)contentHeight
{
    [self.tableView layoutIfNeeded];
    CGFloat contentSizeHeight = self.tableView.contentSize.height;
    return contentSizeHeight;
}

- (void)setTableListNoScrollView
{
    CGFloat height = 0;
    for (HHBaseListItem *item in self.dataSource) {
        height += item.cellHeight;
    }
    self.height = height;
    self.tableView.height = height;
}

- (void)reloadData
{
    [self.tableView reloadData];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HHBaseListItem *item = self.dataSource[indexPath.row];
    if([item isKindOfClass:[HHNormalListItem class]])
    {
        HHNormalListItem *normalItem = (HHNormalListItem *)item;
        if(normalItem.didClickNormalCellBlock)
        {
            normalItem.didClickNormalCellBlock(normalItem);
        }
    }else if([item isKindOfClass:[HHSwitchListItem class]])
    {
        HHSwitchListItem *switchItem = (HHSwitchListItem *)item;
        
    }
}

@end
